<?php

/*
*
* BEGIN matchstatspoller.php
*
*
* Dazzler Server Client Communication Interface - Match Stats Poller
* @Description: This subroutine pushes data to the client when new data is avaialable
* @Authors: Salman Noor, Wits University
* @Copyright: © Wits University 2011 - All Rights Reserved
* @License: BSD License (http://www.linfo.org/bsdlicense.html)
*
*
* BEGIN CHANGELOG
* Developers working on thee files are REQUIRED to maintain this CHANGELOG.
* Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
* !==================================================================================================================================================================
*
* VERSION 1.0.0
*	+ File Created, Salman, 14 October 2011
*
* ==================================================================================================================================================================!
* END CHANGELOG
*/


/*******************************************************************************************************************************************************************
*
* BEGIN Direct Call Check
*
/******************************************************************************************************************************************************************/
global $doNotCallThisScriptDirectly;
if ($doNotCallThisScriptDirectly != 1)
{
	/* Read the ini language file */
	$ini_lang_array = array();
	$clientlang = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
	$lang_ini_array = (file_exists("lang/" . trim($clientlang[0]) . ".ini")) ? parse_ini_file("lang/" . trim($clientlang[0]) . ".ini", true) : parse_ini_file("lang/en.ini", true);
	die ($lang_ini_array['ERRORS']['doNotCallThisScriptDirectly']);
}
/*******************************************************************************************************************************************************************
*
* END Direct Call Check
*
/******************************************************************************************************************************************************************/

$lastmodif    = isset($_REQUEST['matchStatsTimeStamp']) ? $_REQUEST['matchStatsTimeStamp'] : -1;
$GameID = isset($_REQUEST['GameID']) ? $_REQUEST['GameID'] : null;
$Innings = isset($_REQUEST['innings']) ? $_REQUEST['innings'] : null;
$StatsAdmin = new StatsAdmin();
if ($GameID != NULL)
{
  $currentmodif =  $StatsAdmin->getLastBallId($GameID);
}
else
{
       trigger_error("Long Polling parameters are not set properly. Game ID is not set", E_USER_ERROR);
}


while ($currentmodif <= $lastmodif) // check if the data file has been modified
{
  usleep(10000); // sleep 10ms to unload the CPU
  clearstatcache();

    $currentmodif =  $StatsAdmin->getLastBallId($GameID);
}

// return a json array
$response = array();
$MahattanDataInningsOne = $StatsAdmin->getManhattanData($GameID, 1);
$MahattanDataInningsTwo = $StatsAdmin->getManhattanData($GameID, 2);
$RunsTypeInningsOne = $StatsAdmin->getBarGraphData($GameID, 1);
$RunsTypeInningsTwo = $StatsAdmin->getBarGraphData($GameID, 2);
$fallofWicketsInningsOne = $StatsAdmin->getFallOfWickets($GameID, 1);
$fallofWicketsInningsTwo = $StatsAdmin->getFallOfWickets($GameID, 2);
//$battingandBowlingTeamsArray = $StatsAdmin->getBattingAndBowlingTeams($GameID);
//$CurrentScoreArray = $StatsAdmin->getCurrentScore($GameID);
//$CommentaryMessage = $StatsAdmin->getLastBallCommentary($GameID);
$response['manhattanInningsOne']       = $MahattanDataInningsOne;
$response['manhattanInningsTwo']       = $MahattanDataInningsTwo;
$response['runsTypeInningsOne']       = $RunsTypeInningsOne;
$response['runsTypeInningsTwo']       = $RunsTypeInningsTwo;
$response['fallofWicketsInningsOne']       = $fallofWicketsInningsOne;
$response['fallofWicketsInningsTwo']       = $fallofWicketsInningsTwo;
//$response['ballCommentary']       = $CommentaryMessage;
$response['newMatchStatsTimeStamp'] = $currentmodif;
//$response['teams'] = $battingandBowlingTeamsArray;
echo json_encode($response);

flush();

?>
